/**   
* @Title: WarehouseController.java 
* @Package com.csis.system.controller 
* @Description: TODO
* @author dailey  
* @date 2012-10-7
* @version V1.0   
*/
package com.csis.system.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csis.framework.util.jqgrid.JqGridModel;
import com.csis.framework.util.jqgrid.JqGridUtils;
import com.csis.system.common.WebUtil;
import com.csis.system.common.convert.Converter.ConvertArray;
import com.csis.system.domain.WareFormBean;
import com.csis.system.domain.Warehouse;
import com.csis.system.domain.bean.WarehouseBeanFormBean;
import com.csis.system.service.WarehouseService;

/**
 * @author dailey
 *
 */
@Controller
@RequestMapping("/system/ware")
public class WarehouseController {
	protected Log log = LogFactory.getLog(getClass());
	@Resource
	private WarehouseService warehouseService;

	final String BASE_URL = "/system/manager/base/query/";

	final String BASE_URL_2 = "/system/manager/admin/ware/";

	@RequestMapping("/list")
	public String list() {

		return BASE_URL + "listware";
	}

	@RequestMapping("/_list")
	public String _list() {

		return BASE_URL_2 + "listware";
	}

	@RequestMapping("/listBy")
	public void listBy(JqGridModel model, HttpServletResponse response) {
		WareFormBean formBean = WebUtil.buildConvert(model).convertToSingle(WareFormBean.class);
		warehouseService.list(formBean);
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter os = null;
		try {
			os = response.getWriter();
			os.write(JqGridUtils.getJSONParser(WareFormBean.class).parse(formBean).getContent());
			os.flush();
		} catch (Exception e) {
			if (log.isErrorEnabled())
				log.error(e);
			else
				e.printStackTrace();
		} finally {
			if (os != null)
				os.close();
		}
	}

	@RequestMapping("/listCount")
	public void listLeftProductCount(JqGridModel model, HttpServletResponse response) {
		WarehouseBeanFormBean formBean = WebUtil.buildConvert(model).convertToSingle(WarehouseBeanFormBean.class);
		formBean.setWareId(model.getId());
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter os = null;
		try {
			warehouseService.listWareLeft(formBean);
			os = response.getWriter();
			os.write(JqGridUtils.getJSONParser(WarehouseBeanFormBean.class).parse(formBean).getContent());
			os.flush();
		} catch (Exception e) {
			if (log.isErrorEnabled())
				log.error(e);
			else
				e.printStackTrace();
		} finally {
			if (os != null)
				os.close();
		}
	}

	@RequestMapping("/toView")
	public String view(WareFormBean formBean, Model model) {
		Warehouse warehouse = formBean.getBean();
		warehouse = warehouseService.load(warehouse.getId());
		formBean.setBean(warehouse);
		model.addAttribute("formBean", formBean);
		return BASE_URL + "detailware";
	}

	@RequestMapping("/toAdd")
	public String add(Model model, HttpSession session) {
		WareFormBean formBean = new WareFormBean();
		Warehouse bean = new Warehouse();
		formBean.setBean(bean);
		model.addAttribute("formBean", formBean);
		return BASE_URL_2 + "add";
	}

	@RequestMapping("/add")
	public @ResponseBody
	String add(WareFormBean formBean) {
		String result = "0";
		Warehouse bean = formBean.getBean();
		if (bean.getWareName() == null || "".equals(bean.getWareName().trim())) {
			return result;
		}
		try {
			warehouseService.save(bean);
			result = "1";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/toEdit")
	public String edit(Model model, WareFormBean formBean) {
		Integer id = formBean.getBean().getId();
		Warehouse bean = warehouseService.load(id);
		formBean.setBean(bean);
		model.addAttribute("formBean", formBean);
		return BASE_URL_2 + "modify";
	}

	@RequestMapping("/edit")
	public @ResponseBody
	String edit(WareFormBean formBean) {
		return add(formBean);
	}

	@RequestMapping("/toDel")
	public @ResponseBody
	String remove(WareFormBean formBean) {
		String beanIds = formBean.getBeanIds();
		ConvertArray<Integer> ids = WebUtil.buildConvert(beanIds.split(",")).convertToArray(int.class);
		try {
			warehouseService.remove(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		return "1";
	}

	@RequestMapping("/toAssign")
	public String assisgn(WareFormBean formBean, Model model) {
		Warehouse bean = formBean.getBean();
		if (bean != null && bean.getId() != null) {
			bean = warehouseService.load(bean.getId());
			formBean.setBean(bean);
		}
		model.addAttribute("formBean", formBean);
		return BASE_URL_2 + "assign";
	}

	@RequestMapping("/assign")
	public @ResponseBody
	String assisgn(WareFormBean formBean) {
		String result = "0";
		Warehouse bean = formBean.getBean();
		String beanIds = formBean.getBeanIds();
		if (bean == null || bean.getId() == null || !StringUtils.hasLength(beanIds)) {
			return result;
		}
		try {
			bean = warehouseService.load(bean.getId());
			ConvertArray<Integer> ids = WebUtil.buildConvert(beanIds.split(",")).convertToArray(int.class);
			warehouseService.assignUser(bean, ids);
			result = "1";
		} catch (Exception e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

	@RequestMapping("/check")
	public @ResponseBody
	String checkExist(@RequestParam String exist) throws UnsupportedEncodingException {
		String name = "";
		if (exist != null) {
			name = URLDecoder.decode(exist, "UTF-8");
		}
		int size = warehouseService.getWaresByName(name).size();
		return size > 0 ? "1" : "0";
	}
}
